package com.ktjy.ch15.service.impl;

import com.ktjy.ch15.entity.Supplier;
import com.ktjy.ch15.entity.SysRole;
import com.ktjy.ch15.mapper.SupplierMapper;
import com.ktjy.ch15.service.SupplierService;
import com.ktjy.ch15.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public List<Supplier> findAll() {
        return supplierMapper.getAll();
    }

    @Override
    public Page<Supplier> findPage(Supplier supplier, Integer pageNo, Integer pageSize) {
        Page<Supplier> page = new Page<Supplier>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 查询总数，计算总页数
        Integer count = supplierMapper.count(supplier);
        page.setTotalCount(count);
        // 查询分页数据
        Integer pageBegin=(pageNo-1)*pageSize;
        List<Supplier> data = supplierMapper.getPage(supplier, pageBegin, pageSize);
        page.setData(data);
        return page;
    }

    @Override
    public boolean save(Supplier supplier) {
        return supplierMapper.insert(supplier) > 0;
    }

    @Override
    public Supplier findById(Long id) {
        return supplierMapper.getById(id);
    }

    @Override
    public boolean update(Supplier supplier) {
        return supplierMapper.update(supplier) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return supplierMapper.delete(id) > 0;
    }

    @Override
    public Supplier findBySupCode(String supCode) {
        return supplierMapper.getBySupCode(supCode);
    }

}
